<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
class About extends Base{
    public function message(){
        if(!empty(input("param.cus_id"))){
            $data['cus_id']=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.name"))){
            $data['name']=input("param.name");
        }else{
            return json(['code' => 503, 'msg' => '姓名呢？']);exit();
        }
        if(!empty(input("param.tel"))){
            $data['tel']=input("param.tel");
        }else{
            return json(['code' => 504, 'msg' => '电话呢？']);exit();
        }
        if(!empty(input("param.email"))){
            $data['email']=input("param.email");
        }else{
            return json(['code' => 505, 'msg' => '邮箱呢？']);exit();
        }
        if(!empty(input("param.message"))){
            $data['message']=input("param.message");
        }else{
            return json(['code' => 506, 'msg' => '留言呢？']);exit();
        }
        $customer=Db::name('customer')->where('id',$data['cus_id'])->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $data['time']=time();
                $message=Db::name('message')->insert($data);
                if($message){
                    return json(['code' => 200, 'msg' => '成功！']);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $data['time']=time();
                $message=Db::name('message')->insert($data);
                if($message){
                    return json(['code' => 200, 'msg' => '成功！']);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==3&&$customer['platformtt_state']==1){
                $data['time']=time();
                $message=Db::name('message')->insert($data);
                if($message){
                    return json(['code' => 200, 'msg' => '成功！']);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    public function appointments(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $tableset=Db::name('tableset')->where('cus_id',$cus_id)->find();
            if($tableset){
                $tableset['txtconfig']=json_decode($tableset['txtconfig']);
                for ($i=0; $i <count($tableset['txtconfig']) ; $i++) { 
                    $tableset['txtconfig'][$i]->val='';
                    if($i==0){
                        $tableset['txtconfig'][$i]->name='uname';
                    }
                    if($i==1){
                        $tableset['txtconfig'][$i]->name='uphone';
                    }
                    if($i==2){
                        $tableset['txtconfig'][$i]->name='uemail';
                    }
                    if($i==3){
                        $tableset['txtconfig'][$i]->name='uaddress';
                    }
                }
                $tableset['taconfig']=json_decode($tableset['taconfig']);
                $tableset['taconfig']->name='message';
                $tableset['iaconfig']=json_decode($tableset['iaconfig']);
                $tableset['iaconfig']->value=explode(',',$tableset['iaconfig']->value);
                $tableset['iaconfig']->name='uredio';
                $tableset['icconfig']=json_decode($tableset['icconfig']);
                $tableset['icconfig']->value=explode(',',$tableset['icconfig']->value);
                $tableset['icconfig']->name='ucheck';
                return json(['code' => 200, 'msg' => '成功！','data'=>$tableset]);exit();
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    public function toappointment(){
        if(!empty(input("param.cus_id"))){
            $data['cus_id']=input("param.cus_id");
        }else{
            return json(['code' => 503, 'msg' => '请给出客户id']);exit();
        }
        if(input("param.uname")){
            $data['uname']=input("param.uname");
        }else{
            return json(['code' => 504, 'msg' => '姓名呢？']);exit();
        }
        if(input("param.uphone")){
            $data['uphone']=input("param.uphone");
        }else{
            return json(['code' => 505, 'msg' => '电话呢？']);exit();
        }
        if(input("param.uemail")){
            $data['uemail']=input("param.uemail");
        }else{
            return json(['code' => 506, 'msg' => '邮箱呢？']);exit();
        }
        if(input("param.uaddress")){
            $data['uaddress']=input("param.uaddress");
        }else{
            return json(['code' => 507, 'msg' => '地址呢？']);exit();
        }
        if(input("param.uredio")){
            $data['uredio']=input("param.uredio");
        }else{
            return json(['code' => 508, 'msg' => '单选值呢？']);exit();
        }
        if(input("param.ucheck")){
            $data['ucheck']=input("param.ucheck");
        }else{
            return json(['code' => 509, 'msg' => '多选值呢？']);exit();
        }
        if(input("param.message")){
            $data['message']=input("param.message");
        }else{
            return json(['code' => 510, 'msg' => '留言呢？']);exit();
        }
        $customer=Db::name('customer')->where('id',$data['cus_id'])->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $data['time']=time();
                $message=Db::name('appointment')->insert($data);
                if($message){
                    $emailinfo=Db::name('tableset')->where('cus_id',$data['cus_id'])->find();
                    $name=$emailinfo['email_code'];
                    $subject='业务咨询';
                    $tmdtitle=json_decode($emailinfo['txtconfig'],true);
                    $allinfo=$tmdtitle[0]['title'].'：'.$data['uname'].'；<br>'.$tmdtitle[1]['title'].'：'.$data['uphone'].'；<br>'.$tmdtitle[2]['title'].'：'.$data['uemail'].'；<br>'.$tmdtitle[3]['title'].'：'.$data['uaddress'].'；<br>单选值：'.$data['uredio'].'；<br>多选值：'.$data['ucheck'].'；<br>留言：'.$data['message'];
                    $this->send_mail($emailinfo['email_self'],$name,$subject,$allinfo);
                    return json(['code' => 200, 'msg' => '成功！']);exit();
                }else{
                    return json(['code' => 500, 'msg' => '失败！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $data['time']=time();
                $message=Db::name('appointment')->insert($data);
                if($message){
                    $emailinfo=Db::name('tableset')->where('cus_id',$data['cus_id'])->find();
                    $name=$emailinfo['email_code'];
                    $subject='业务咨询';
                    $tmdtitle=json_decode($emailinfo['txtconfig'],true);
                    $allinfo=$tmdtitle[0]['title'].'：'.$data['uname'].'；<br>'.$tmdtitle[1]['title'].'：'.$data['uphone'].'；<br>'.$tmdtitle[2]['title'].'：'.$data['uemail'].'；<br>'.$tmdtitle[3]['title'].'：'.$data['uaddress'].'；<br>单选值：'.$data['uredio'].'；<br>多选值：'.$data['ucheck'].'；<br>留言：'.$data['message'];
                    $this->send_mail($emailinfo['email_self'],$name,$subject,$allinfo);
                    return json(['code' => 200, 'msg' => '成功！']);exit();
                }else{
                    return json(['code' => 500, 'msg' => '失败！']);exit();
                }
            }elseif(input("param.platform")==3&&$customer['platformtt_state']==1){
                $data['time']=time();
                $message=Db::name('appointment')->insert($data);
                if($message){
                    $emailinfo=Db::name('tableset')->where('cus_id',$data['cus_id'])->find();
                    $name=$emailinfo['email_code'];
                    $subject='业务咨询';
                    $tmdtitle=json_decode($emailinfo['txtconfig'],true);
                    $allinfo=$tmdtitle[0]['title'].'：'.$data['uname'].'；<br>'.$tmdtitle[1]['title'].'：'.$data['uphone'].'；<br>'.$tmdtitle[2]['title'].'：'.$data['uemail'].'；<br>'.$tmdtitle[3]['title'].'：'.$data['uaddress'].'；<br>单选值：'.$data['uredio'].'；<br>多选值：'.$data['ucheck'].'；<br>留言：'.$data['message'];
                    $this->send_mail($emailinfo['email_self'],$name,$subject,$allinfo);
                    return json(['code' => 200, 'msg' => '成功！']);exit();
                }else{
                    return json(['code' => 500, 'msg' => '失败！']);exit();
                }
            }else{
                return json(['code' => 501, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }

    private function send_mail($tomail, $name, $subject = '', $body = '', $attachment = null) {

        $mail = new \PHPMailer\PHPMailer\PHPMailer();           //实例化PHPMailer对象
        $mail->CharSet = 'UTF-8';           //设定邮件编码，默认ISO-8859-1，如果发中文此项必须设置，否则乱码
        $mail->IsSMTP();                    // 设定使用SMTP服务
        $mail->SMTPDebug = 0;               // SMTP调试功能 0=关闭 1 = 错误和消息 2 = 消息
        $mail->SMTPAuth = true;             // 启用 SMTP 验证功能
        $mail->SMTPSecure = 'ssl';          // 使用安全协议
        $mail->Host = "smtp.qq.com"; // SMTP 服务器
        $mail->Port = 465;                  // SMTP服务器的端口号
        $mail->Username = $tomail;    // SMTP服务器用户名
        $mail->Password =$name;     // SMTP服务器密码，这里是你开启SMTP服务时生成密码
        $mail->SetFrom($tomail, '自己!');
        $replyEmail = '';                   //留空则为发件人EMAIL
        $replyName = '';                    //回复名称（留空则为发件人名称）
        $mail->AddReplyTo($replyEmail, $replyName);
        $mail->Subject = $subject;
        $mail->MsgHTML($body);
        $mail->AddAddress($tomail,'自己');
        if (is_array($attachment)) { // 添加附件
            foreach ($attachment as $file) {
                is_file($file) && $mail->AddAttachment($file);
            }
        }
        return $mail->Send() ? true : $mail->ErrorInfo;
    }
    // public function tmdemail(){
        // $toemail='@qq.com';//收件人邮箱
        // $name='自己';//接收邮件者名称
        // $subject='业务咨询';//邮件主题
        // $content='姓名：xx；联系方式：911';//邮件内容
        // //调用方法发送邮件
        // $this->send_mail($toemail,$name,$subject,$content);
    // }
    public function tmdtest(){

                    $emailinfo=Db::name('tableset')->where('cus_id',1)->find();
                    $tmd=json_decode($emailinfo['txtconfig'],true);
                    return json(['code' => 200, 'msg' => $tmd[0]['title']]);exit();
    }
}